﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Configuration;

namespace AssemblyMerger {
    class Program {
        static readonly string[] _excludeList = new[] { 
            "Newtonsoft.Json.dll", "RabbitMQ.Client.dll",
            "ServiceStack.Redis.dll", "ServiceStack.Text.dll", "ServiceStack.Interfaces.dll",
            "ServiceStack.Common.dll", "Thrift.dll", "Apache.Cassandra.dll", "Cassandraemon.dll",
            "PServiceBus.WindowsPhone.dll", "PServiceBus.dll", "BookSleeve.dll"
        };
        static readonly bool _deleteMergedFiles = Boolean.Parse(ConfigurationManager.AppSettings["DeleteMergedFiles"]);

        static void Main(string[] args) {
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            //Merge main assemblies
            InvokeILMerge(@"/targetplatform:v4,c:\Windows\Microsoft.NET\Framework\v4.0.30319 /allowDup:PoweredByAttribute /out:Binaries/PServiceBus.dll Binaries/PServiceBus.Core.dll Binaries/Phoenix.ReflectionExtensions.dll Binaries/PServiceBus.Gateway.Interfaces.dll Binaries/PServiceBus.Gateway.ZeroMQ.dll Binaries/PServiceBus.Gateway.Redis.dll Binaries/PServiceBus.EmailTransport.dll Binaries/PServiceBus.MSMQ.dll Binaries/PServiceBus.MSSQLTransport.dll Binaries/PServiceBus.RabbitMQ.dll Binaries/RabbitMQ.Client.dll Binaries/PServiceBus.Redis.dll Binaries/PServiceBus.Cassandra.dll Binaries/Apache.Cassandra.dll Binaries/Cassandraemon.dll Binaries/Thrift.dll Binaries/PServiceBus.Services.Gateway.dll Binaries/PServiceBus.Services.Web.dll Binaries/PServiceBus.Tcp.dll Binaries/PServiceBus.Ftp.dll Binaries/PServiceBus.Transports.dll Binaries/PServiceBus.WCF.NetTcpTransport.dll Binaries/PServiceBus.Workflow.Activities.dll Binaries/Phoenix.ZeroMQConnectors.dll Binaries/Phoenix.RedisConnectors.dll Binaries/clrzmq.dll Binaries/MessageShark.dll Binaries/ServiceStack.Text.dll Binaries/ServiceStack.Redis.dll Binaries/ServiceStack.Interfaces.dll Binaries/ServiceStack.Common.dll Binaries/PServiceBus.Serializer.Xml.dll Binaries/PServiceBus.MessageProxy.dll Binaries/PServiceBus.Client.dll");

            //Merge window phone assemblies
            InvokeILMerge(@"/targetplatform:v4,""%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71"" /allowDup:PoweredByAttribute /out:Binaries/PServiceBus.WindowsPhone.dll Binaries/PServiceBus.WindowsPhone.dll Binaries/Json.WindowsPhone.dll");

            //Merge silverlight assemblies
            InvokeILMerge(@"/targetplatform:v4,""%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0"" /allowDup:PoweredByAttribute /out:Binaries/PServiceBus.Silverlight.dll Binaries/PServiceBus.Silverlight.dll Binaries/Json.WindowsPhone.dll");

            if (_deleteMergedFiles) {
                var dir = new DirectoryInfo("Binaries");
                var files = dir.GetFiles().Where(file => !_excludeList.Contains(file.Name)).ToList();
                try {
                    files.ForEach(file => file.Delete());
                } catch { }
            }
        }
        static void InvokeILMerge(string arguments){
            Process.Start(new ProcessStartInfo()
            {
                Arguments = arguments,
                FileName = "ILMerge.exe",
                UseShellExecute = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true
            }).WaitForExit();
        }
    }
}
